package com.knowif.pojo;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotEmpty;

import org.springframework.validation.annotation.Validated;

@Validated
public class FuzzySearch {
    private Integer id;

    @NotEmpty(message = "模糊查询内容不能为空")
    private String searchContent;

    @NotEmpty(message = "查询字段属性不能为空")
    private String searchName;

    @NotEmpty(message = "用户id不能为空")
    private Integer userId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getSearchContent() {
        return searchContent;
    }

    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent == null ? null : searchContent.trim();
    }

    public String getSearchName() {
        return searchName;
    }

    public void setSearchName(String searchName) {
        this.searchName = searchName == null ? null : searchName.trim();
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    
    //将tutorInfo里面的数据转化为fuzzySearch表中
    public static List<FuzzySearch> Tutor2FuzzySearch(TutorInfo tutorInfo){
    	if(tutorInfo == null){
    		return null;
    	}
    	List<FuzzySearch> fuzzySearchs = new ArrayList<FuzzySearch>();
    	//先转换学校
		List<UserEducationExperience> userEducationExperiences = tutorInfo.getUserEducationExperiences();
		if(userEducationExperiences != null && userEducationExperiences.size() > 0){
			for(UserEducationExperience userEducationExperience : userEducationExperiences){
				fuzzySearchs.addAll(UserEducationExperience2FuzzySearch(userEducationExperience, tutorInfo.getUserId()));
			}
		}
		List<UserItem> userItems = tutorInfo.getUserItem();
		if(userItems != null && userItems.size() > 0){
			userItems.forEach(userItem -> {
				if(userItem.getTutorEducationInfo() != null){
					FuzzySearch fuzzySearch = new FuzzySearch();
					fuzzySearch.setUserId(tutorInfo.getUserId());
					fuzzySearch.setSearchName("school");
					fuzzySearch.setSearchContent(userItem.getTutorEducationInfo().getSchoolName());
					fuzzySearchs.add(fuzzySearch);
				}
			});
		}

		//在转换用户信息
    	fuzzySearchs.addAll(TUser2FuzzySearch(tutorInfo.gettUser()));

		if(tutorInfo.getLabel() != null && tutorInfo.getLabel().size() > 0){
			//将lable转化为模糊查询数据
			for(Label label : tutorInfo.getLabel()){
				fuzzySearchs.addAll(label2FuzzySearch(label));
			}
		}

		if(tutorInfo.getTopic() != null && tutorInfo.getTopic().size() > 0) {
			//将topic转化为模糊查询数据
			for(Topic topic : tutorInfo.getTopic()){
				fuzzySearchs.addAll(topic2FuzzySearch(topic));
			}
		}
    	
		return fuzzySearchs;
    }
    
    
    /**
     * @author: xingyao 
     * @Description:  标签转化为模糊查询数据
     * @param lable
     * @return
     */
    public static List<FuzzySearch> label2FuzzySearch(Label lable){
    	if(lable == null){
    		return new ArrayList<FuzzySearch>();
    	}
    	List<FuzzySearch> fuzzySearchs = new ArrayList<FuzzySearch>();
    	
    	FuzzySearch label = new FuzzySearch();
    	label.setSearchContent(lable.getLabelName());
    	label.setSearchName("lable");
    	label.setUserId(lable.getUserId());
    	fuzzySearchs.add(label);
    	return fuzzySearchs;
    }
    
    
    /**
     * @author: xingyao 
     * @Description:  
     * @param userEducationExperience
     * @return
     */
    public static List<FuzzySearch> UserEducationExperience2FuzzySearch(UserEducationExperience userEducationExperience,int userId){
    	if(userEducationExperience == null){
    		return new ArrayList<FuzzySearch>();
    	}
    	List<FuzzySearch> fuzzySearchs = new ArrayList<FuzzySearch>();
    	
    	FuzzySearch school = new FuzzySearch();
    	school.setSearchContent(userEducationExperience.getSchoolName());
    	school.setSearchName("school");
    	school.setUserId(userId);
    	fuzzySearchs.add(school);
    	
    	
    	FuzzySearch subject = new FuzzySearch();
    	subject.setSearchContent(userEducationExperience.getSubject());
    	subject.setSearchName("subject");
    	subject.setUserId(userId);
    	fuzzySearchs.add(subject);
    	
    	
    	FuzzySearch college = new FuzzySearch();
    	college.setSearchContent(userEducationExperience.getCollege());
    	college.setSearchName("college");
    	college.setUserId(userId);
    	fuzzySearchs.add(college);
    	
    	
    	FuzzySearch schoolShortName = new FuzzySearch();
    	schoolShortName.setSearchContent(userEducationExperience.getSchoolName());
    	schoolShortName.setSearchName("schoolShortName");
    	schoolShortName.setUserId(userId);
    	fuzzySearchs.add(schoolShortName);
    	
    	return fuzzySearchs;
    	
    }
    
    
    public static List<FuzzySearch> TUser2FuzzySearch(TUser tUser){
    	if(tUser == null){
    		return new ArrayList<>();
    	}
    	
    	List<FuzzySearch> fuzzySearchs = new ArrayList<FuzzySearch>();
    	
    	FuzzySearch nickName = new FuzzySearch();
    	nickName.setSearchContent(tUser.getFullName());
    	nickName.setSearchName("fullName");
    	nickName.setUserId(tUser.getId());
    	fuzzySearchs.add(nickName);

    	return fuzzySearchs;
    }
    
    
    
    public static List<FuzzySearch> topic2FuzzySearch(Topic topic){
    	if(topic == null){
    		return new ArrayList<FuzzySearch>();
    	}
    	
    	List<FuzzySearch> fuzzySearchs = new ArrayList<FuzzySearch>();
    	
    	FuzzySearch topicName = new FuzzySearch();
    	topicName.setSearchContent(topic.getTopicName());
    	topicName.setSearchName("topicName");
    	topicName.setUserId(topic.getUserId());
    	fuzzySearchs.add(topicName);
    	
    	return fuzzySearchs;
    }
    
    
    public static List<Integer> getUserIds(List<FuzzySearch> fuzzySearchs){
    	if(fuzzySearchs == null || fuzzySearchs.isEmpty()){
    		return new ArrayList<Integer>();
    	}
    	List<Integer> userIds = new ArrayList<Integer>();
		for (FuzzySearch fuzzySearch : fuzzySearchs) {
			userIds.add(fuzzySearch.getUserId());
		}
		return userIds;
    }
    
    
    public static void saveHighlight(List<FuzzySearch> fuzzySearchs, List<TutorInfo> tutorInfos) {
		for(FuzzySearch fuzzySearch : fuzzySearchs){
			for (TutorInfo tutorInfo : tutorInfos) {
				if(tutorInfo.getUserId() == fuzzySearch.getUserId()){
					tutorInfo.setHighlight(fuzzySearch.getSearchContent());
				}
			}
		}
	}
    
	@Override
	public String toString() {
		return "FuzzySearch [id=" + id + ", searchContent=" + searchContent + ", searchName=" + searchName + ", userId="
				+ userId + "]";
	}
    
    
}